package com.example.teamtribe.main.club;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.main.club.clubdetail.ClubDetailActivity;
import com.example.teamtribe.main.club.manage.TopManageActivity;
import com.example.teamtribe.utils.Utility;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ClubGridAdapter extends RecyclerView.Adapter {

    private static final int TYPE_PAGER = 0;
    private static final int TYPE_ITEM = 1;
    private Context context;
    private List<Team> teams = new ArrayList<>();

    public void add(Team team) {
        teams.add(team);
        Collections.sort(teams);
    }

    public void addAll(Collection<Team> teams) {
        this.teams.addAll(teams);
        Collections.sort(this.teams);
    }

    public void clear() {
        teams.clear();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == TYPE_PAGER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_pager, parent, false);
            return new PagerViewHolder(view);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_club, parent, false);
        final ItemViewHolder holder = new ItemViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition() - 1;
                Team team = teams.get(position);
                if (team.getRealId() == 123456789) {
                    context.startActivity(new Intent(context, TopManageActivity.class));
                    return;
                }
                Intent intent = new Intent(context, ClubDetailActivity.class);
                intent.putExtra("team", team);
                context.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PagerViewHolder) {
            PagerViewHolder pagerViewHolder = (PagerViewHolder) holder;
            List<Integer> images = new ArrayList<>(3);
            images.add(R.drawable.test_club1);
            images.add(R.drawable.test_club2);
            images.add(R.drawable.test_club3);
            ClubPagerAdapter adapter = new ClubPagerAdapter(images, context);
            pagerViewHolder.vpNews.setAdapter(adapter);
        }
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            Team team = teams.get(position - 1);
            if (team.getRealId() == 123456789) {
                Glide.with(context).load(R.drawable.manage).into(itemViewHolder.ivPortrait);
                itemViewHolder.tvName.setText(team.getName());
                return;
            }
            Glide.with(context).load(Utility.getUri(team.getPortrait())).into(itemViewHolder.ivPortrait);
            itemViewHolder.tvName.setText(team.getName());
        }
    }

    @Override
    public int getItemCount() {
        return teams.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_PAGER;
        return TYPE_ITEM;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = (GridLayoutManager) manager;
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position == 0) {
                        return 3;
                    }
                    return 1;
                }
            });
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        ImageView ivPortrait;
        TextView tvName;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ivPortrait = itemView.findViewById(R.id.iv_item_club);
            tvName = itemView.findViewById(R.id.tv_item_club);
        }
    }

    static class PagerViewHolder extends RecyclerView.ViewHolder {

        RollPagerView vpNews;

        public PagerViewHolder(View itemView) {
            super(itemView);
            vpNews = itemView.findViewById(R.id.viewPager);
            vpNews.setPlayDelay(2000);
            vpNews.setHintView(new ColorPointHintView(itemView.getContext(), R.color.colorAccent, Color.WHITE));
        }
    }
}
